<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="负责人" prop="userId">
          <el-select
            v-model="queryParams.userId"
            placeholder="请选择客户名称"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="user in userList"
              :key="user.userId"
              :label="user.userName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <el-select
            v-model="queryParams.customerId"
            placeholder="请选择客户名称"
            style="width: 100%"
            filterable
            clearable
          >
            <el-option
              v-for="customer in customerList"
              :key="customer.customerId"
              :label="customer.customerName"
              :value="customer.customerId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户行业" prop="customerIndustry">
          <el-select
            style="width: 100%"
            v-model="queryParams.customerIndustry"
            placeholder="请选择客户行业"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_industry"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户级别" prop="customerRank">
          <el-select
            style="width: 100%"
            v-model="queryParams.customerRank"
            placeholder="请选择客户级别"
            clearable
          >
            <el-option
              v-for="dict in dict.type.customer_rank"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户类型" prop="customerType">
          <el-select
            style="width: 100%"
            v-model="queryParams.customerType"
            placeholder="请选择客户类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.crm_customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <el-cascader
            v-model="queryParams.region"
            :options="cityTree"
            :props="cascaderProps"
            size="small"
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['crm:customer:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['crm:customer:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="handleClick"
        ></right-toolbar>
      </el-row>
    </el-card>

    <div style="margin-top: 15px">
      <!-- <el-card class="body-box"> -->
      <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
        <el-tab-pane label="全部客户" name="customerList" lazy>
          <customerList ref="customerList" :options="options"></customerList>
        </el-tab-pane>
        <el-tab-pane label="核心客户" name="myCustomerList" lazy>
          <myCustomerList ref="myCustomerList" :options="options"></myCustomerList>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import customerList from "./customerList.vue";
import myCustomerList from "./myCustomerList.vue";
import { mapGetters } from "vuex";
import {
  getCityTreeAllByParentId,
  selectCityListFromChildToParentId,
} from "@/api/system/city";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";
import { getUserListByStatus } from "@/api/system/user";
export default {
  name: "Customer",
  dicts: [
    "customer_industry",
    "crm_customer_type",
    "customer_rank",
    "customer_status",
    "crm_customer_label",
  ],
  components: {
    customerList,
    myCustomerList,
  },
  props: {
    options: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        customerName: null,
        linkman: null,
        phone: null,
        region: null,
        customerIndustry: null,
        customerRank: null,
        customerType: null,
        customerLabel: null,
        customerStatus: null,
      },
      activeName: "customerList",
      cityTree: [],
      cascaderProps: {
        value: "label", // 选项的ID字段
        label: "label", // 选项的显示字段
        children: "children", // 子节点字段
        checkStrictly: true,
      },
      userList: []
    };
  },
  computed: {
    ...mapGetters(["device"]),
  },
  created() {
    this.getCrmCustomerAllNoPage()
    this.getUserListByStatus()
    this.getCityTree();
    this.$nextTick(() => {
      if (this.device !== "mobile") {
        this.$refs[this.activeName].getList(this.queryParams);
      }
    });
  },

  methods: {
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    getCrmCustomerAllNoPage() {
      getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    getCityTree() {
      getCityTreeAllByParentId(1).then((response) => {
        this.cityTree = response.data;
        this.cityTree = this.moveJiangxiToFirst(this.cityTree)
      });
    },
    handleClick(tab, event) {
      this.$nextTick(() => {
        this.$refs[this.activeName].getList(this.queryParams);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$nextTick(() => {
        this.$refs[this.activeName].getList(this.queryParams);
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs[this.activeName].open();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "crm/customer/export",
        {
          ...this.queryParams,
        },
        `customer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.list-todo {
  color: white;
  background-color: #1c8ef0;
  padding: 15px;
  border-radius: 10px;
}

.waterfall-layout {
  column-count: 2;
  column-gap: 15px;
}

.customer-card {
  margin-bottom: 15px;
  break-inside: avoid;
}

.body-box {
  max-height: 65vh;
  overflow: auto;
}
</style>
